package com.zj.core.base

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.support.constraint.ConstraintLayout
import android.support.v7.app.AppCompatActivity
import android.view.KeyEvent
import android.view.MotionEvent
import android.view.View
import com.jaeger.library.StatusBarUtil
import com.systemutils.SoftKeyboardUtils
import com.utils.NetUtils
import com.dialogutils.ToastUtil
import com.fileutils.SPUtils
import com.moduleinterface.OnClickEventListener
import com.quitutils.SwipeBackLayout
import com.quitutils.Utils
import com.quitutils.app.SwipeBackActivityBase
import com.systemutils.SoftKeyBoardEvent.keyClickEvent
import com.zj.core.base.BaseApplication.Companion.mBleToothBind
import com.quitutils.app.SwipeBackActivityHelper
import com.quitutils.app.SwipeBackListenerActivityAdapter
import com.zj.core.bean.UserInfo

/**
 * 基类Activity
 */
@SuppressLint("Registered")
open class BaseActivity :
    AppCompatActivity(),
    OnClickEventListener,
    SwipeBackActivityBase {

    var mHospitalID = ""
    var mDepartmentID = ""

    private var mHelper: SwipeBackActivityHelper? = null
    private var mQiteMode = 0

    private var mSwipLayout: SwipeBackLayout? = null
    private var mIntent: Intent? = null


    @SuppressLint("ObsoleteSdkInt")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
            StatusBarUtil.setTranslucentForCoordinatorLayout(this, 0)
        }
        mIntent = Intent()

        mHospitalID = UserInfo.hospitalID
        mDepartmentID = UserInfo.departmentID
    }

    /**
     * 设置滑动退出模式
     */
    open fun setQuitMode(mode: Int) {
        this.mQiteMode = mode
        createSwipHelper()
    }

    private fun createSwipHelper() {
        if (mQiteMode != 0) {
            mHelper = SwipeBackActivityHelper(this)
            mHelper!!.onActivityCreate()
            mSwipLayout = mHelper!!.swipeBackLayout
            mSwipLayout!!.setSwipListener(mSwipBackListener)
        }
    }

    var mSwipBackListener = SwipeBackListenerActivityAdapter.SwipBackListener { flags -> resultActivityFlgs(flags) }

    open fun resultActivityFlgs(flags: Int) {
        if (!this.isFinishing) {
            this.finish()
            this.overridePendingTransition(0, 0)
        }
    }

    override fun onPostCreate(savedInstanceState: Bundle?) {
        super.onPostCreate(savedInstanceState)
        if (mQiteMode != 0) {
            mHelper!!.onPostCreate()
            mSwipLayout?.setEdgeTrackingEnabled(mQiteMode)
        }
    }

    override fun getSwipeBackLayout(): SwipeBackLayout? {
        return mSwipLayout
    }

    override fun setSwipeBackEnable(enable: Boolean) {
        swipeBackLayout?.setEnableGesture(enable)
    }

    override fun scrollToFinishActivity() {
        Utils.convertActivityToTranslucent(this)
        swipeBackLayout?.scrollToFinishActivity()
    }

    /**
     * 蓝牙连接
     */
    fun connectBle(mac: String) {
        mBleToothBind?.connectBle(mac)
    }

    /**
     * 断开连接
     */
    fun disConnectBle(mac: String) {
        if (mac != "") {
            mBleToothBind?.disConnectBle(mac)
        }
    }

    fun activityJump(ctx: Context, clazz: Class<Any>, bundle: Bundle?) {
        mIntent?.apply {
            setClass(ctx, clazz)
            putExtras(bundle)
            startActivity(this)
        }
    }

    fun activityJumpForResult(ctx: Context, request: Int, clazz: Class<Any>, bundle: Bundle?) {
        mIntent?.apply {
            setClass(ctx, clazz)
            putExtras(bundle)
            startActivityForResult(this, request)
        }
    }

    open fun setTitleBarParams(view: View) {
        view.layoutParams = ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, 100)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        return keyClickEvent(this, keyCode)
    }

    /**
     * 网络检查
     */
    fun netWorkCheck() {
        if (!NetUtils.isConnected(BaseApplication.context)
            || !NetUtils.isAvailable(BaseApplication.context)
        ) {
            ToastUtil.showShort(BaseApplication.context, "网络未连接")
            return
        }
    }

    /**
     * 通过该方法监听Activity是否被操作
     */
    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        return super.dispatchTouchEvent(ev)
    }

    override fun maorenClick() {}

    override fun leftClick() {
        finish()
    }

    override fun centerClick() {
        finish()
    }

    override fun rightClick() {
        finish()
    }

    override fun finish() {
        closeKeyboard()
        super.finish()
    }

    private fun closeKeyboard() {
        if (SoftKeyboardUtils.isSoftShowing(this)) {
            SoftKeyboardUtils.hideSystemSoftKeyboard(this, currentFocus.windowToken)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        mIntent = null
    }
}